<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>定时任务</title>
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
<script>
// 新增
function add(){
	$(":checked").attr("checked",false);
	document.formName.reset();
	$("#id").val("");
    dia("新增任务");
}

//编辑管理员
 function edit(id,name,desc,cron){
	$(":checkbox").attr("checked",false);
	
	$("#id").val(id);
	$("#jobName").val(name);
	$("#desc").val(desc);
	$("#cronExpression").val(cron);
	dia("编辑任务");
} 

function startjob(id){
	$.ajax({
		type:"get",
		dataType:"json",
		url:"/quartz/o_start.do",
		data:{id:id},
	    beforeSend:function(){
		    art.dialog.tips("开始中...");
	    },
	   success: function(data){//成功的时候调用
		   if(data.success){
			   location.reload();
		   }else{
			   error_msg(data.msg , function(){location.reload();});
		   }
	   }
	});
}

function closejob(id){
	$.ajax({
		type:"get",
		dataType:"json",
		url:"/quartz/o_close.do",
		data:{id:id},
	    beforeSend:function(){
		    art.dialog.tips("关闭中...");
	    },
	   success: function(data){//成功的时候调用
		   if(data.success){
			   location.reload();
		   }else{
			   error_msg(data.msg , function(){location.reload();});
		   }
	   }
	});
}

function startonce(id) {
	art.dialog.alert("立即执行一次?",function () {
		art.dialog.post({
			url:"/quartz/o_once.do",
			data:{id:id},
            success:function (data) {
				if(data.success){
					art.dialog.tips("执行成功");
				}
            }
		});
    })
}

function dia(title){//弹窗
   dialog = art.dialog({
        title: title,
        width:500,
        lock:true,
        content: document.getElementById('create')
    });
}

function del(id){
	v_confirm('是否确认删除' , function(){
		$.ajax({
			   type: "get",
			   dataType:"json",
			   url: "/quartz/o_del.do",
			   data:{id:id},
			   beforeSend:function(){
				    art.dialog.tips("删除中...");
			   },
			   success: function(data){//成功的时候调用
				   if(data.success){
					   location.reload();
				   }else{
					   error_msg(data.msg , function(){location.reload();});
				   }
			   }
		});
	})
}



function sub(){
	$(this).attr("disabled","disabled");
	if($("#signupForm").valid()){
	$.ajax({
		   type: "POST",
		   dataType:"json",
		   url: "/quartz/o_add.do",
		   data:$("form").serialize(), //序列表表格内容为字符串
		   beforeSend:function(){
			   art.dialog.tips("加载中.....");
		   },
		   success: function(data){//成功的时候调用
			   if(data.success){
				   location.reload();
			   }
		   }
	});
	
};
}
$().ready(function(){
	 //验证表单
    $("#signupForm").validate({
		showErrors: function(map, list) {
			var focussed = document.activeElement;
			this.currentElements.removeAttr("rel").removeClass("valid_false").removeClass("valid_true");
			$.each(list, function(index, error) {
				$(error.element).attr("rel", error.message).addClass("valid_false").powerFloat({targetMode: "remind",zIndex:9999});
			});
			$.each(this.successList, function(index, suc) {
				$(suc).removeAttr("rel").addClass("valid_true");
			});
		},
    	rules:{
    		name:{
    			required:true,
    			maxlength:10
    		}
    	}
    });
});
</script>
</head>
<body>

	<div class="panel panel-info">
		<div class="panel-heading">
			  <button type="button"  onclick="add();" class="btn btn-info"><span class="glyphicon glyphicon-plus"></span>&nbsp;新增</button>
		</div>
		<table class="table table-hover table-bordered">
			<tr>
				<th width="50px">#</th>
				<th>任务名称</th>
				<th>任务描述</th>
				<th >表达式</th>
				<th >状态</th>
				<th >操作</th>
			</tr>
        	
			[#list tasks as task]
			<tr>
				<td>${task_index+1}</td>
				<td>${task.job_name!}</td>
				<td>${task.job_desc!}</td>
				<td>${task.cron_expression!}</td>
				<td>[#if task.job_status==0 ]未启用[#elseif task.job_status==1 ]已启用[#else][/#if]${task.job_status!0}</td>
				<td >
					<input value="编辑" type="button" onclick="edit(${task.id},'${task.job_name}','${task.job_desc}','${task.cron_expression!}');" class="btn btn-primary btn-xs"/>
				[#if task.job_status==0  ]
					<input value="开启" type="button" onclick="startjob(${task.id});" class="btn btn-primary btn-xs"/>
				[#else]
					<input value="关闭" type="button" onclick="closejob(${task.id});" class="btn btn-primary btn-xs"/>
				[/#if]
					<input type="button" value="立即执行" class="btn btn-primary btn-xs" onclick="startonce(${task.id})"  >
				</td>
			</tr>
			[/#list]
		</table>

	</div>

	<!--新增管理员 -->

	<div id="create" style="display: none;" >
		<form  id="signupForm" class="form-horizontal" name="formName" role="form">
			<input type="hidden" name="id" id="id" />
			<div class="form-group">
				<label for="name">任务名称:</label> <input class="form-control" placeholder="spring bean's name!"   id="jobName" name="job_name" required />
			</div>

			<div class="form-group">
				<label for="name">描述:</label> <input class="form-control" id="desc" name="job_desc" />
			</div>
            
			<div class="form-group">
				<label for="name">表达式:</label> <input class="form-control" id="cronExpression" name="cron_expression" />
			</div>
            
        	
			<div class="form-group" style="margin-top: 10px;"  >
				<div style="text-align: center;">
				   <button  onclick="sub();"  type="button" class="btn btn-info">提交</button>
				</div>
			</div>
		</form>
	</div>

</body>
</html>